package com.glela.order.service.impl.order.amqp;

import org.apache.http.entity.ContentType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageBuilder;
import org.springframework.amqp.core.MessageListener;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.core.MessagePropertiesBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import com.glela.cache.redis.RedisUtil;
import com.glela.common.util.DateUtil;
import com.glela.common.util.JsonUtil;
import com.glela.common.util.NetworkUtil;
import com.glela.mq.service.OrderQueueService;
import com.glela.order.model.OrderSettlementCommision;
import com.glela.order.service.OrderSettlementCommisionService;

/**
 * **********************************************
 * 
 * @Title: 0元付订单队列监听
 * @throws @author
 *             gaoweiqi
 * @version 0.0.0.1
 * @since 2018年5月4日 下午1:05:35
 * 
 *        **********************************************
 */
public class WaitingCommision implements MessageListener {

	private static Logger logger = LoggerFactory.getLogger(WaitingCommision.class);
	private final static Long SLEEPTIME = 2000L;
	public static final String APPID = "小铺来了";
	@Autowired
	private OrderQueueService orderQueueService;
	@Autowired
	private OrderSettlementCommisionService orderSettlementCommisionService;
	@Autowired
	RedisUtil redisUtil;

	public void onMessage(Message message) {
		final String orderSn = new String(message.getBody());
		logger.error("消费队列：Message:{}", orderSn);
		int times = 10;
		while ((times--) > 0) {
			OrderSettlementCommision orderSettlementCommision = null;
			try {
				Thread.sleep(SLEEPTIME);// 延迟执行
				orderSettlementCommision = orderSettlementCommisionService.getOrderSettlementCommisionByorderSn(orderSn);
				if (orderSettlementCommision != null && orderSettlementCommision.getStatus() == 3) {// 加入消息队列
					orderSettlementCommision.setErrorMsg("0元付进入队列处理");
					orderQueueService.addMQPayCallBack(createMessageProperties(orderSn, JsonUtil.toJson(orderSettlementCommision)));
					break;
				}
			} catch (Exception e) {
				logger.error("支付成功后添加结算单消息队列失败：{}", e.getMessage());
				orderSettlementCommisionService.updateStatus("定时任务处理", 2, 3, DateUtil.getNow(), orderSettlementCommision.getId());
				break;
			}
		}
	}

	protected Message createMessageProperties(String MessageID, String body) {
		MessageProperties MessageProperties = MessagePropertiesBuilder.newInstance().build();
		MessageProperties.setAppId(APPID);
		MessageProperties.setContentType(ContentType.APPLICATION_JSON.getMimeType());
		MessageProperties.setContentEncoding(ContentType.APPLICATION_JSON.getCharset().name());
		MessageProperties.setContentLength(body.length());
		MessageProperties.setMessageId(MessageID);
		MessageProperties.setHeader("serverIP", NetworkUtil.getLocalIP());
		return MessageBuilder.withBody(body.getBytes()).andProperties(MessageProperties).build();
	}
}
